package com.wbs.shop.nuby.utils;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;

public class FragmentChangeManager {
    private FragmentManager mFragmentManager;
    private int mContainerViewId;
    /**
     * Fragment切换数组
     */
    private ArrayList<Fragment> mFragments;
    /**
     * 当前选中的Tab
     */
    private int mCurrentTab;

    public FragmentChangeManager(FragmentManager fm, int containerViewId, ArrayList<Fragment> fragments) {
        this.mFragmentManager = fm;
        this.mContainerViewId = containerViewId;
        this.mFragments = fragments;
        initFragments();
    }

    /**
     * 初始化fragments
     */
    private void initFragments() {
        Fragment fragment;
        String tag;
        for (int i = 0; i < mFragments.size(); i++) {
            fragment = mFragments.get(i);
            tag = String.format("%s-%s", fragment.getClass().getName(), i);
            Fragment fragmentByTag = mFragmentManager.findFragmentByTag(tag);
            if (fragmentByTag != null){
                mFragments.set(i,fragmentByTag);
                mFragmentManager.beginTransaction().hide(fragmentByTag).commit();
            }else {
                mFragmentManager.beginTransaction().add(mContainerViewId, fragment, tag).hide(fragment).commit();
            }
        }
        setFragments(0);
    }

    /**
     * 界面切换控制
     */
    public void setFragments(int index) {
        if (index == -1)
            return;
        for (int i = 0; i < mFragments.size(); i++) {
            FragmentTransaction ft = mFragmentManager.beginTransaction();
            Fragment fragment = mFragments.get(i);
            if (i == index) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commitAllowingStateLoss();
        }
        mCurrentTab = index;
    }

    public void removeAll() {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        for (int i = 0; i < mFragments.size(); i++) {
            ft.remove(mFragments.get(i));
        }
        ft.commitAllowingStateLoss();
    }

    public int onAttachFragment(Fragment fragment) {
        if (fragment != null && mFragments != null) {
            for (int i = 0; i < mFragments.size(); i++) {
                if (mFragments.get(i).getClass().isAssignableFrom(fragment.getClass())) {
                    mFragments.set(i, fragment);
                    return i;
                }
            }
        }
        return -1;
    }

    public int getCurrentTab() {
        return mCurrentTab;
    }

    public Fragment getCurrentFragment() {
        return mFragments.get(mCurrentTab);
    }
    public <T extends Fragment> T getCurrentFragment(Class<T> clz) {
        return (T) mFragments.get(mCurrentTab);
    }
    public ArrayList<Fragment> getFragments() {
        return mFragments;
    }
}
